package com.pms.web.controller;


import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.pms.utils.MD5;
import com.pms.web.model.AdminModel;
import com.pms.web.service.LoginBase;

@Controller
public class LoginAction {
	@Resource(name = "loginS110")
	private LoginBase ls;

	@RequestMapping(value = "login.pms")
	public String login() {
		return "login";
	}

	/**
	 * 根据结果重定向
	 * 
	 * @param username
	 * @param password
	 * @param mv
	 * @return
	 */
	@RequestMapping(value = "loginAction.pms", method = RequestMethod.POST)
	public ModelAndView loginAction(String username, String password,
			ModelAndView mv,HttpServletResponse resp) {
		if (null != username && null != password) {
			String securyPassword=MD5.getMD5Code(password);
			AdminModel um=ls.getInfo(username, securyPassword);
				if(um!=null){
					if(securyPassword.equals(um.getAdmin_password())){
						mv.addObject("timestamp", System.currentTimeMillis());
						Cookie userid=new Cookie("userid",um.getAdmin_id()+"");
						userid.setMaxAge(60*60);
						Cookie permission=new Cookie("permission",um.getAdmin_valid()+"");
						permission.setMaxAge(60*60);
						Cookie nickname=new Cookie("username",um.getAdmin_username());
						userid.setMaxAge(60*60);
						resp.addCookie(userid);
						resp.addCookie(permission);
						resp.addCookie(nickname);
						mv.setViewName("redirect:index.pms");
						return mv;
					}
				}
				
			}
		mv.setViewName("redirect:login.pms");
		return mv;

	}
}
